//设备信息
var _this;
Vue.filter('timeFormat', function(value) {
    let ret;
    let date = new Date(value);
    let fmt = "YYYY-mm-dd HH:MM";
    const opt = {
        "Y+": date.getFullYear().toString(), // 年
        "m+": (date.getMonth() + 1).toString(), // 月
        "d+": date.getDate().toString(), // 日
        "H+": date.getHours().toString(), // 时
        "M+": date.getMinutes().toString(), // 分
        "S+": date.getSeconds().toString() // 秒
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        };
    };
    return fmt;
});
var app = new Vue({
    el: '#myModal',
    mounted() {
        _this = this;
        let siteInfo = getStorage("siteInfo");
        this.userId = getStorage("loginUserId");
        this.id = siteInfo.id;
        this.name = siteInfo.name;
        this.cid = siteInfo.cid;
    },
    data: {
        userId: '', //用户编号
        id: '', //站点编号
        name: '', //站点名称
        cid: '', //客户编号
        linkStatus: '', //联网状态
        totalCount: 200, //总项目数
        arrPageSize: [10, 20, 30, 40], //分页数
        pageCount: 20, //当前分页数
        pageCurrent: 1, //当前页面
        pagesize: 10, //分页大小
        showPages: 5, //显示分页按钮数
        showPagesStart: 1, //开始显示的分页按钮
        showPageEnd: 100, //结束显示的分页按钮
        arrayDataAll: [], //所有数据
        arrayData: [], //分页数据
    },
    methods: {
        /**
         * 分页方法
         * @param {页码} pageIndex 
         */
        showPage: function(pageIndex, refresh) {
            if (pageIndex <= 0) return;
            if (pageIndex > this.pageCount) {
                pageIndex = this.pageCount;
            }
            //判断数据是否需要更新
            var currentPageCount = Math.ceil(this.totalCount / this.pagesize);
            if (currentPageCount != this.pageCount) {
                pageIndex = 1;
                this.pageCount = currentPageCount;
            } else if (this.pageCurrent == pageIndex && currentPageCount == this.pageCount && typeof(refresh) == "undefined") {
                return;
            }

            //处理分页点中样式
            var buttons = $("#pager").find("span");
            for (var i = 0; i < buttons.length; i++) {
                if (buttons.eq(i).html() != pageIndex) {
                    buttons.eq(i).removeClass("active");
                } else {
                    buttons.eq(i).addClass("active");
                }
            }
            //从所有数据中取分页数据
            var newPageInfo = [];
            for (var i = 0; i < this.pagesize; i++) {
                var index = i + (pageIndex - 1) * this.pagesize;
                if (index > this.totalCount - 1) break;
                newPageInfo[newPageInfo.length] = this.arrayDataAll[index];
            }
            this.pageCurrent = pageIndex;
            this.arrayData = newPageInfo;

            if (this.pageCount > this.showPages) {
                if (pageIndex <= (this.showPages - 1) / 2) {
                    this.showPagesStart = 1;
                    this.showPageEnd = this.showPages - 1;
                } else if (pageIndex >= this.pageCount - (this.showPages - 3) / 2) {
                    this.showPagesStart = this.pageCount - this.showPages + 2;
                    this.showPageEnd = this.pageCount;
                } else {
                    this.showPagesStart = pageIndex - (this.showPages - 3) / 2;
                    this.showPageEnd = pageIndex + (this.showPages - 3) / 2;
                }
            }
        },
    }
});